package com.yj.ccs.abs.common.service;

import com.yj.ccs.abs.common.Constants;
import com.yj.ccs.abs.common.enums.FileType;
import com.yj.ccs.abs.util.storage.StorageUtil;
import com.yj.ccs.abs.web.common.entity.Attachment;
import com.yj.ccs.abs.web.common.service.CommonService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigInteger;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/16
 * Author     : Unbelievable9
 **/
@Service
public class FileService {

    @Value("${storage.file_path}")
    private String serverPath;

    @Value("${storage.base_url}")
    private String baseUrl;

    private final LocalStorageService localStorageService;

    private final CommonService commonService;

    @Autowired
    public FileService(LocalStorageService localStorageService,
                       CommonService commonService) {
        this.localStorageService = localStorageService;
        this.commonService = commonService;
    }


    public Boolean checkFileSize(MultipartFile multipartFile) {
        BigInteger fileSize = StorageUtil.calcFileSize(multipartFile.getSize(), StorageUtil.FileSizeUnits.MB);

        return fileSize.compareTo(BigInteger.valueOf(Constants.MAX_UPLOAD_FILE_SIZE_MB)) < 1;
    }

    public Attachment uploadFile(Integer type, MultipartFile file) throws IOException {
        FileType fileType = FileType.fromValue(type);

        if (fileType != null) {
            String bucket;

            switch (fileType) {
                case COMPANY_LICENSE:
                    bucket = Constants.COMPANY_LICENSE_BUCKET;
                    break;
                case CLIENT_EQUITY:
                    bucket = Constants.CLIENT_EQUITY_BUCKET;
                    break;
                case CLIENT_LICENSE:
                    bucket = Constants.CLIENT_LICENSE_BUCKET;
                    break;
                case RECEIPT:
                    bucket = Constants.RECEIPT_BUCKET;
                    break;
                case SETTLE_ACCOUNT:
                    bucket = Constants.SETTLE_ACCOUNT_BUCKET;
                    break;
                case FUND_RESTREAM:
                    bucket = Constants.FUND_RESTREAM_BUCKET;
                    break;
                case REMARK:
                    bucket = Constants.REMARK_BUCKET;
                    break;
                case BASIC_INSURED:
                    bucket = Constants.BASIC_INSURED_BUCKET;
                    break;
                case BASIC_GUARANTEE:
                    bucket = Constants.BASIC_GUARANTEE_BUCKET;
                    break;
                case BASIC_DEPOSITED:
                    bucket = Constants.BASIC_DEPOSITED_BUCKET;
                    break;
                case BASIC_EXPANDED:
                    bucket = Constants.BASIC_EXPANDED_BUCKET;
                    break;
                case CONTRACT_FILE:
                    bucket = Constants.CONTRACT_FILE_BUCKET;
                    break;
                case CONTRACT_CONSIGNED:
                    bucket = Constants.CONTRACT_CONSIGNED_BUCKET;
                    break;
                case CONTRACT_DELIVERED:
                    bucket = Constants.CONTRACT_DELIVERED_BUCKET;
                    break;
                case CONTRACT_RECKONED:
                    bucket = Constants.CONTRACT_RECKONED_BUCKET;
                    break;
                case PO_PROPERTY:
                    bucket = Constants.PO_PROPERTY_BUCKET;
                    break;
                case PO_TOOL:
                    bucket = Constants.PO_TOOL_BUCKET;
                    break;
                case PO_TRANSPORT:
                    bucket = Constants.PO_TRANSPORT_BUCKET;
                    break;
                case PO_SHEET:
                    bucket = Constants.PO_SHEET_BUCKET;
                    break;
                case PO_LABORATORY:
                    bucket = Constants.PO_LABORATORY_BUCKET;
                    break;
                case PO_CERT:
                    bucket = Constants.PO_CERT_BUCKET;
                    break;
                    default:
                        bucket = null;
            }

            Attachment attachment = new Attachment();
            attachment.setUrl(baseUrl + "/uploads" + serverPath + StorageUtil.save(localStorageService, file, bucket));
            attachment.setName(FilenameUtils.getBaseName(file.getOriginalFilename()));
            attachment.setType(FilenameUtils.getExtension(file.getOriginalFilename()));

            return attachment;
        } else {
            return null;
        }
    }
}
